package top.shiyiri.stream;

import org.junit.Test;

import java.io.*;

/**
 * @author Aunean
 * @date 2022/1/9
 */
/*
1.缓冲流
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter

2.作用：提升流的读取、写入的速度
    原因：内部提供了一个缓冲区

3.处理流，就是 “套接” 在已有的流的基础上
 */
public class BufferedTest {

    public void bufferedInputOutputStream(String srcPath, String destPath) {
        //1.实例化File类
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        //2.造流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            //2.1 造节点流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //2.2 造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //3.操作数据
            byte[] bytes = new byte[1024];
            int len;

            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
                //bos.flush();//刷新缓冲区
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流：后用先关。先关闭外层的流，再关闭内层的流
            try {
                assert bos != null;
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //说明关闭外层流的同时，内存流也会自动的进行关闭。
            //关于内存流的关闭，我们可以省略
            //fos.close();
            //fis.close();
        }

    }

    @Test
    public void testBufferedInputOutputStream() {
        long startTime = System.currentTimeMillis();

        String srcPath = "C:\\Users\\14533\\Pictures\\QQplayerPic\\毕设录屏.mp4";
        String destPath = "C:\\Users\\14533\\Pictures\\QQplayerPic\\复制.mp4";

        bufferedInputOutputStream(srcPath, destPath);

        long endTime = System.currentTimeMillis();
        System.out.println("复制操作所花费的时间为：" + (endTime - startTime));//140
    }

    /*
    使用 BufferedReader 和 BufferedWriter 实现文本文件的复制
     */
    @Test
    public void testBufferedReaderBufferedWriter() {
        //1.创建文件和响应的流
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(new File("dbcp.txt")));
            bw = new BufferedWriter(new FileWriter(new File("dbcp2.txt")));

            //2.读写操作
            //方式一：使用 char[] 数组
/*            char[] cbuf = new char[1024];
            int len;
            while ((len = br.read(cbuf)) != -1) {
                bw.write(cbuf, 0, len);
            }*/

            //方式二：使用 String
            String data;
            while ((data = br.readLine()) != null) {
                //readLine()：不包含换行
                //方式一：换行
                bw.write(data + "\n");

                //方式二：换行
                //bw.write(data);
                bw.newLine();//提供换行的操作
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //3.关闭流
            try {
                assert bw != null;
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
